<template>
  <a-modal
    class="noborder"
    :title="title"
    :width="520"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
<!--        <a-form-item label="会员ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <span>{{ record.zone_id }}</span>
        </a-form-item>-->
        <a-form-item label="员工姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--          <span>{{ record.name }}</span>-->
          <a-input v-decorator="['name', { rules: [{ required: true, min: 2, message: '请输入至少2个字符' }] }]" />
        </a-form-item>
        <a-form-item label="员工电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['mobile', { rules: [{ required: true, min: 11, message: '请输入至少11位' }] }]" />
        </a-form-item>

        <a-form-item label="所在部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-cascader
              v-decorator="['zone_id', { rules: [{ required: true }] }]"

              :options="zoneList"
              change-on-select
              placeholder="Please select"
              @change="handleChange"

          >
          </a-cascader>
        </a-form-item>


      </a-form>
    </a-spin>
  </a-modal>
</template>


<script>
import pick from 'lodash.pick'
import * as Api from '@/api/user'

export default {
  components: {
  },
  props: {
    // 会员等级列表
    zoneList: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      // 对话框标题
      title: '编辑员工信息',
      // 标签布局属性
      labelCol: { span: 7 },
      // 输入框布局属性
      wrapperCol: { span: 13 },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录
      record: {

      },
    }
  },

  methods: {
    handleChange(value, selectedOptions) {
      console.log(value,selectedOptions)
      // console.log(`selected ${selectedOptions.map(item => item.name).join(', ')}`, value);
    },
    // 显示对话框
    handle (record) {
      // 显示窗口
      this.visible = true
      // 当前记录
      this.record = record
      // 设置默认值
      this.setFieldsValue(record)
    },

    // 设置默认值
    setFieldsValue (record) {
      // const { record, form: { setFieldsValue } } = this
      this.$nextTick( async () => {


     

       const id = await this.getParentsById(this.zoneList,record.zone_id)
        if( id)    {
           this.record.zone_id = id
        }

        this.form.setFieldsValue(pick(this.record, ['name','mobile','zone_id']))

      })

    },


    // 确认按钮
    handleSubmit (e) {

      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        !errors && this.onFormSubmit(values)
      })
    },

    // 关闭对话框事件
    handleCancel () {
      this.visible = false
      this.form.resetFields()
    },

    // 提交到后端api
    onFormSubmit (values) {
      this.confirmLoading = true
      const sendData = values
      sendData.zone_id = values.zone_id[values.zone_id.length - 1]
      Api.edit({ userId: this.record.user_id, form: sendData })
        .then(result => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框事件
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally(() => this.confirmLoading = false)
    },
    // 部门数据回显
    getParentsById(list,id){


  for (let i in list) {
    if (list[i].value == id){
            
      return [list[i].value]
    }

    if (list[i].children) {
      let node = this.getParentsById(list[i].children, id)
      if (node !== undefined) {
//追加父节点
        node.unshift(list[i].value)
        return node
      }
    }
  }
},

  }
}
</script>
<style lang="less">
.ant-form-item {
  margin-bottom: 15px;
}
</style>
